package vtc;

import java.util.*;
import java.util.concurrent.*;
import java.io.File; 
import java.io.IOException;

import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RefineryUtilities;

import com.ib.client.Contract;
import com.ib.client.Order;
import com.tictactec.ta.lib.*;

import jxl.*;
import jxl.read.biff.*;

public class TestProcessingUnit 
{
	
	static void test4() 
	{
		VtcArrayBlockingQueue q0 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq0 = new VtcArrayBlockingQueue[1];
		vq0[0] = q0;
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		FunctionalUnitExample f0 = new FunctionalUnitExample("p0", vq0, vq1);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[1];
		vq2[0] = q2;
		FunctionalUnitExample f1 = new FunctionalUnitExample("p1", vq1, vq2);
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
					
		for(Integer i = 0; i < 100; i++)
		{
			Date date = new Date();
			VtcTimeTaggedEvent event = new VtcTimeTaggedEvent(i, date); 
			try
			{
				vq0[0].forcePut(event);
			}
			catch(InterruptedException e)
			{
				System.out.print("Interrupted during forcePut(event) in test4()");
			}
		}
		
		for(Integer i = 0; i < 100; i++)
		{
			try
			{
				VtcTimeTaggedEvent event = vq2[0].take();
				Integer j = (Integer)event.getEvent();
				System.out.print("Output queue event = " + j + "\n");
			}
			catch(InterruptedException e)
			{
				System.out.print("Interrupted during forcePut(event) in test4()");
			}
		}
		try
		{
			TimeUnit.SECONDS.sleep(1);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in test()");
		}
		exec.shutdownNow();
	}

	static void test5() 
	{
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		XlsGoogleDateOpenInputUnit f0 = new XlsGoogleDateOpenInputUnit("p0", vq1, "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", 100);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[1];
		vq2[0] = q2;
		FunctionalUnitExample f1 = new FunctionalUnitExample("p1", vq1, vq2);
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
					
		for(Integer i = 0; i < 100; i++)
		{
			try
			{
				VtcTimeTaggedEvent event = vq2[0].take();
				Double j = (Double)event.getEvent();
				System.out.print("Output queue event = " + j + "\n");
			}
			catch(InterruptedException e)
			{
				System.out.print("Interrupted during forcePut(event) in test4()");
			}
		}
		try
		{
			TimeUnit.SECONDS.sleep(1);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in test()");
		}
		exec.shutdownNow();
	}

	static void test6() 
	{
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		XlsGoogleDateOpenInputUnit f0 = new XlsGoogleDateOpenInputUnit("p0", vq1, "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", 100);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue q3 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue q4 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[3];
		vq2[0] = q2;
		vq2[1] = q3;
		vq2[2] = q4;
		FanOutUnit f1 = new FanOutUnit("p1", vq1, vq2);
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
					
		for(Integer i = 0; i < 100; i++)
		{
			try
			{
				VtcTimeTaggedEvent event1 = vq2[0].take();
				Double j1 = (Double)event1.getEvent();
				VtcTimeTaggedEvent event2 = vq2[1].take();
				Double j2 = (Double)event2.getEvent();
				VtcTimeTaggedEvent event3 = vq2[2].take();
				Double j3 = (Double)event3.getEvent();
				System.out.print("Output queue event = " + j1 + "," + j2 + "," + j3 + "\n");
			}
			catch(InterruptedException e)
			{
				System.out.print("Interrupted during forcePut(event) in test4()");
			}
		}
		try
		{
			TimeUnit.SECONDS.sleep(1);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in test()");
		}
		exec.shutdownNow();
	}

	static void test7() 
	{
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		XlsGoogleDateOpenInputUnit f0 = new XlsGoogleDateOpenInputUnit("p0", vq1, "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", 100);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[1];
		vq2[0] = q2;
		FunctionalUnitExample f1 = new FunctionalUnitExample("p1", vq1, vq2);
		
		OutputUnitExample f2 = new OutputUnitExample("p2", vq2);
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
		exec.execute(f2);
		
		try
		{
			TimeUnit.SECONDS.sleep(5);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in test()");
		}
		exec.shutdownNow();
	}
	
	static void test8() 
	{
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(15);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		XlsGoogleDateOpenInputUnit f0 = new XlsGoogleDateOpenInputUnit("p0", vq1, "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", 100);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(15);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[1];
		vq2[0] = q2;
		EMA f1 = new EMA("p1", vq1, vq2, 10);
		
		OutputUnitExample f2 = new OutputUnitExample("p2", vq2);
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
		exec.execute(f2);
		
		try
		{
			TimeUnit.SECONDS.sleep(1);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in test()");
		}
		exec.shutdownNow();
	}
	
	/**
	 * 
	 */
	static void test9() 
	{
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(50);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		XlsGoogleDateOpenInputUnit f0 = new XlsGoogleDateOpenInputUnit("p0", vq1, "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", 25);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(50);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[1];
		vq2[0] = q2;
		EMA f1 = new EMA("p1", vq1, vq2, 30);
		
		DrawUnit f2 = new DrawUnit("p2", vq2, 50, "Draw Unit example", "Date", "Open price");
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
		exec.execute(f2);
		
		try
		{
			TimeUnit.MILLISECONDS.sleep(50000);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in test()");
		}
		exec.shutdownNow();
	}
	
	static void test10() 
	{
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		XlsGoogleDateOpenInputUnit f0 = new XlsGoogleDateOpenInputUnit("p0", vq1, "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", 100);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue q3 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[2];
		vq2[0] = q2;
		vq2[1] = q3;
		FanOutUnit f1 = new FanOutUnit("p1", vq1, vq2);
		
		VtcArrayBlockingQueue q4 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq3 = new VtcArrayBlockingQueue[1];
		vq3[0] = q4;
		FunctionalUnitExample f2 = new FunctionalUnitExample("p2", vq2, vq3);
		
		VtcArrayBlockingQueue [] vq4 = new VtcArrayBlockingQueue[2];
		vq4[0] = q4;
		vq4[1] = q3;
		VtcArrayBlockingQueue q5 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq5 = new VtcArrayBlockingQueue[1];
		vq5[0] = q5;
		SynchronizationUnitExample f3 = new SynchronizationUnitExample("p3", vq4, vq5);
		
		OutputUnitExample f4 = new OutputUnitExample("p4", vq5);
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
		exec.execute(f2);
		exec.execute(f3);
		exec.execute(f4);
				
		try
		{
			TimeUnit.SECONDS.sleep(5);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in test10()");
		}
		exec.shutdownNow();
	}
	
	static void test11() 
	{
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		XlsGoogleDateOpenInputUnit f0 = new XlsGoogleDateOpenInputUnit("p0", vq1, "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", 10);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue q3 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[2];
		vq2[0] = q2;
		vq2[1] = q3;
		FanOutUnit f1 = new FanOutUnit("p1", vq1, vq2);
		
		VtcArrayBlockingQueue q4 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq3 = new VtcArrayBlockingQueue[1];
		vq3[0] = q4;
		FunctionalUnitExample f2 = new FunctionalUnitExample("p2", vq2, vq3);
		
		VtcArrayBlockingQueue [] vq4 = new VtcArrayBlockingQueue[2];
		vq4[0] = q4;
		vq4[1] = q3;
		VtcArrayBlockingQueue q5 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq5 = new VtcArrayBlockingQueue[1];
		vq5[0] = q5;
		SynchronizationUnitExample f3 = new SynchronizationUnitExample("p3", vq4, vq5);
		
		DrawDoubleDateVectorQueue f4 = new DrawDoubleDateVectorQueue("p4", vq5, 100, "DrawDoubleDateVectorQueue Example", "Date", "Price", 2);
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
		exec.execute(f2);
		exec.execute(f3);
		exec.execute(f4);
				
		try
		{
			TimeUnit.SECONDS.sleep(50);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in test10()");
		}
		exec.shutdownNow();
	}
	
	static void test12() 
	{
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		XlsGoogleDateOpenInputUnit f0 = new XlsGoogleDateOpenInputUnit("p0", vq1, "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", 50);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(50);
		VtcArrayBlockingQueue q3 = new VtcArrayBlockingQueue(50);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[2];
		vq2[0] = q2;
		vq2[1] = q3;
		FanOutUnit f1 = new FanOutUnit("p1", vq1, vq2);
		
		VtcArrayBlockingQueue q4 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq3 = new VtcArrayBlockingQueue[1];
		vq3[0] = q4;
		//FunctionalUnitExample f2 = new FunctionalUnitExample("p2", vq2, vq3);
		EMA f2 = new EMA("p2", vq2, vq3, 10);
		
		
		VtcArrayBlockingQueue [] vq4 = new VtcArrayBlockingQueue[2];
		vq4[0] = q4;
		vq4[1] = q3;
		VtcArrayBlockingQueue q5 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq5 = new VtcArrayBlockingQueue[1];
		vq5[0] = q5;
		SynchronizationUnitExample f3 = new SynchronizationUnitExample("p3", vq4, vq5);
		
		DrawDoubleDateVectorQueue f4 = new DrawDoubleDateVectorQueue("p4", vq5, 200, "DrawDoubleDateVectorQueue Example", "Date", "Price", 2);
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
		exec.execute(f2);
		exec.execute(f3);
		exec.execute(f4);
				
		try
		{
			TimeUnit.SECONDS.sleep(50);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in test10()");
		}
		exec.shutdownNow();
	}
	
	static void test13() 
	{
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		XlsGoogleDateOpenInputUnit f0 = new XlsGoogleDateOpenInputUnit("p0", vq1, "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", 50);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(50);
		VtcArrayBlockingQueue q3 = new VtcArrayBlockingQueue(50);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[2];
		vq2[0] = q2;
		vq2[1] = q3;
		FanOutUnit f1 = new FanOutUnit("p1", vq1, vq2);
		
		VtcArrayBlockingQueue q4 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq3 = new VtcArrayBlockingQueue[1];
		vq3[0] = q4;
		//FunctionalUnitExample f2 = new FunctionalUnitExample("p2", vq2, vq3);
		MAX f2 = new MAX("p2", vq2, vq3, 10);
		
		
		VtcArrayBlockingQueue [] vq4 = new VtcArrayBlockingQueue[2];
		vq4[0] = q4;
		vq4[1] = q3;
		VtcArrayBlockingQueue q5 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq5 = new VtcArrayBlockingQueue[1];
		vq5[0] = q5;
		SynchronizationUnitExample f3 = new SynchronizationUnitExample("p3", vq4, vq5);
		
		DrawDoubleDateVectorQueue f4 = new DrawDoubleDateVectorQueue("p4", vq5, 100, "DrawDoubleDateVectorQueue Example", "Date", "Price", 2);
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
		exec.execute(f2);
		exec.execute(f3);
		exec.execute(f4);
				
		try
		{
			TimeUnit.SECONDS.sleep(50);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in test10()");
		}
		exec.shutdownNow();
	}
	
	static void test14() 
	{
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		XlsGoogleDateOpenInputUnit f0 = new XlsGoogleDateOpenInputUnit("p0", vq1, "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", 50);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(50);
		VtcArrayBlockingQueue q3 = new VtcArrayBlockingQueue(50);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[2];
		vq2[0] = q2;
		vq2[1] = q3;
		FanOutUnit f1 = new FanOutUnit("p1", vq1, vq2);
		
		VtcArrayBlockingQueue q4 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq3 = new VtcArrayBlockingQueue[1];
		vq3[0] = q4;
		//FunctionalUnitExample f2 = new FunctionalUnitExample("p2", vq2, vq3);
		EMA f2 = new EMA("p2", vq2, vq3, 10);
		
		
		VtcArrayBlockingQueue [] vq4 = new VtcArrayBlockingQueue[2];
		vq4[0] = q4;
		vq4[1] = q3;
		VtcArrayBlockingQueue q5 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq5 = new VtcArrayBlockingQueue[1];
		vq5[0] = q5;
		SynchronizationUnitExample f3 = new SynchronizationUnitExample("p3", vq4, vq5);
		
		VtcArrayBlockingQueue q6 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq6 = new VtcArrayBlockingQueue[1];
		vq6[0] = q6;
		StrategyUnitExample  f4 = new StrategyUnitExample("p4", vq5, vq6);
		
		VtcArrayBlockingQueue q7 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq7 = new VtcArrayBlockingQueue[1];
		vq7[0] = q7;
		VtcFSM fsm = new VtcFSM("closed");
		fsm.AddState("open");
		try
		{
			fsm.addTransition("closed", "buy", "open");
			fsm.addTransition("open", "sell", "closed");
		}
		catch(VtcFSMException e)
		{
			System.out.print(e.getMessage());
		}
		FSM f5 = new FSM("p5", vq6, vq7, fsm);
				
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
		exec.execute(f2);
		exec.execute(f3);
		exec.execute(f4);
		exec.execute(f5);
				
		try
		{
			TimeUnit.SECONDS.sleep(50);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in test10()");
		}
		exec.shutdownNow();
	}
	
	static void test15() 
	{
		IBContractFactory contractFactory = new IBContractFactory();
		IBOrderFactory orderFactory = new IBOrderFactory();
		Contract contract = contractFactory.makeCashContract("EUR", "USD");
		Order order = orderFactory.makeContract("BUY", 21000);
		IBBroker broker = new IBBroker();
		broker.connect("", 7496, 0);
		if(broker.isConnected())
		{
			broker.reqManagedAccts();
			broker.reqAccountUpdates(true, "");
			broker.setServerLogging(5);
			try
			{
				TimeUnit.MILLISECONDS.sleep(1000);
			}
			catch(InterruptedException e)
			{
				System.out.print("Interrupted during sleep in test15()");
			}
			int orderId = broker.getNextValidId();
			System.out.print("next id:" + orderId + "\n");
			broker.reqAccountUpdates(true, "");
			for(int i=orderId; i<orderId+100; i++)
			{
				broker.placeOrder(i, contract, order);
				System.out.print("Iter: " + i + "\n");
				try
				{
					TimeUnit.MILLISECONDS.sleep(500);
				}
				catch(InterruptedException e)
				{
					System.out.print("Interrupted during sleep in test15()");
				}
			}
			broker.disconnect();
		}
	}
	
	static void test16() 
	{
		
		IBContractFactory contractFactory = new IBContractFactory();
		IBOrderFactory orderFactory = new IBOrderFactory();
		Contract contract = contractFactory.makeStockContract("QLD", "SMART");//.makeCashContract("EUR", "USD");
		IBBroker broker = new IBBroker();
		broker.connect("", 7496, 0);
		if(broker.isConnected())
		{
			VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(10);
			VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
			vq1[0] = q1;
			IBFeedUnit feed = new IBFeedUnit("feed1", vq1, broker, contract, "", true);
			ExecutorService exec = Executors.newCachedThreadPool();
			exec.execute(feed);
			System.out.print("5\n");
			try
			{
				TimeUnit.MILLISECONDS.sleep(20000);
			}
			catch(InterruptedException e)
			{
				System.out.print("Interrupted during sleep in test16()");
			}
			exec.shutdownNow();
			broker.disconnect();
		}
	}
	
	static void backtest() 
	{
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		XlsGoogleDateOpenInputUnit f0 = new XlsGoogleDateOpenInputUnit("p0", vq1, "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", 50);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(50);
		VtcArrayBlockingQueue q3 = new VtcArrayBlockingQueue(50);
		VtcArrayBlockingQueue q8 = new VtcArrayBlockingQueue(50);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[3];
		vq2[0] = q2;
		vq2[1] = q3;
		vq2[2] = q8;
		FanOutUnit f1 = new FanOutUnit("p1", vq1, vq2);
		
		VtcArrayBlockingQueue q4 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq3 = new VtcArrayBlockingQueue[1];
		vq3[0] = q4;
		//FunctionalUnitExample f2 = new FunctionalUnitExample("p2", vq2, vq3);
		EMA f2 = new EMA("p2", vq2, vq3, 10);
		
		
		VtcArrayBlockingQueue [] vq4 = new VtcArrayBlockingQueue[2];
		vq4[0] = q4;
		vq4[1] = q3;
		VtcArrayBlockingQueue q5 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq5 = new VtcArrayBlockingQueue[1];
		vq5[0] = q5;
		SynchronizationUnitExample f3 = new SynchronizationUnitExample("p3", vq4, vq5);
		
		VtcArrayBlockingQueue q6 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq6 = new VtcArrayBlockingQueue[1];
		vq6[0] = q6;
		StrategyUnitExampleIB  f4 = new StrategyUnitExampleIB("p4", vq5, vq6);
		
		VtcArrayBlockingQueue q7 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq7 = new VtcArrayBlockingQueue[1];
		vq7[0] = q7;
		DummyIBBrokerUnit f5 = new DummyIBBrokerUnit("p5", vq6, vq7, 1000000);
				
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
		exec.execute(f2);
		exec.execute(f3);
		exec.execute(f4);
		exec.execute(f5);
				
		try
		{
			TimeUnit.SECONDS.sleep(50);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in backtest()");
		}
		exec.shutdownNow();
	}
	
	static void backtest2() 
	{
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(1);
		VtcArrayBlockingQueue [] vq1 = new VtcArrayBlockingQueue[1];
		vq1[0] = q1;
		XlsGoogleDateOpenInputUnit f0 = new XlsGoogleDateOpenInputUnit("p0", vq1, "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", 50);
		
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue q3 = new VtcArrayBlockingQueue(1);
		VtcArrayBlockingQueue q8 = new VtcArrayBlockingQueue(1);
		VtcArrayBlockingQueue [] vq2 = new VtcArrayBlockingQueue[3];
		vq2[0] = q2;
		vq2[1] = q3;
		vq2[2] = q8;
		FanOutUnit f1 = new FanOutUnit("p1", vq1, vq2);
		
		VtcArrayBlockingQueue q4 = new VtcArrayBlockingQueue(100);
		VtcArrayBlockingQueue [] vq3 = new VtcArrayBlockingQueue[1];
		vq3[0] = q4;
		//FunctionalUnitExample f2 = new FunctionalUnitExample("p2", vq2, vq3);
		EMA f2 = new EMA("p2", vq2, vq3, 10);
		
		
		VtcArrayBlockingQueue [] vq4 = new VtcArrayBlockingQueue[2];
		vq4[0] = q4;
		vq4[1] = q3;
		VtcArrayBlockingQueue q5 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq5 = new VtcArrayBlockingQueue[1];
		vq5[0] = q5;
		SynchronizationUnitExample f3 = new SynchronizationUnitExample("p3", vq4, vq5);
		
		VtcArrayBlockingQueue q6 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq6 = new VtcArrayBlockingQueue[2];
		vq6[0] = q6;
		StrategyUnitExampleIB  f4 = new StrategyUnitExampleIB("p4", vq5, vq6);
		
		vq6[1] = q8;
		VtcArrayBlockingQueue q7 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq7 = new VtcArrayBlockingQueue[1];
		vq7[0] = q7;
		SynchronizationUnitExample f5 = new SynchronizationUnitExample("p5", vq6, vq7);
		
		VtcArrayBlockingQueue q9 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue [] vq8 = new VtcArrayBlockingQueue[1];
		vq8[0] = q9;
		DummyIBBrokerUnit f6 = new DummyIBBrokerUnit("p6", vq7, vq8, 1000000);
				
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(f0);
		exec.execute(f1);
		exec.execute(f2);
		exec.execute(f3);
		exec.execute(f4);
		exec.execute(f5);
		exec.execute(f6);
				
		try
		{
			TimeUnit.SECONDS.sleep(50);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in backtest()");
		}
		exec.shutdownNow();
	}
	
	static void testTalib() 
	{
		Core talib = new Core();
		double[] data = new double[100];
		double[] output = new double[100];
		MInteger outBeg = new MInteger();
		MInteger outNbElement = new MInteger();
		
		try
		{
			Workbook workbook = Workbook.getWorkbook(new File("C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls"));
			Sheet sheet = workbook.getSheet(0);
			Cell cell;
			
			for(Integer i = 1; i < 101; i++)
			{
				cell = sheet.getCell(1,i);
				if(cell.getType() == CellType.NUMBER) 
				{ 
					NumberCell nc = (NumberCell) cell; 
					double number = nc.getValue();
					System.out.print("position: " + (i-1) + " value: " + number + "\n");
					data[i-1] = number;
					output[i-1] = 0.0;
				}
			}
		}
		catch(BiffException e)
		{
			System.out.print("BiffException");
		}
		catch(IOException e)
		{
			System.out.print("IOException" + e.getMessage());
		}
		finally
		{
			talib.ema(90, 99, data, 10, outBeg, outNbElement, output);
			System.out.print("outBeg: " + outBeg.value + "\n");
			System.out.print("outNbElement: " + outNbElement.value + "\n");
			for(Integer i = 0; i < 100; i++)
			{
				System.out.print("position: " + i + " value: " + output[i] + "\n");
			}
		}
		
		
	}
		
	static void test() 
	{
		// Unlimited size blocking queues
		VtcArrayBlockingQueue q0 = new VtcArrayBlockingQueue(10);
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(10);
		ProcessingUnitExample p0 = new ProcessingUnitExample("p0", q0, q1);
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(10);
		ProcessingUnitExample p1 = new ProcessingUnitExample("p1", q1, q2);
		VtcArrayBlockingQueue q3 = new VtcArrayBlockingQueue(10);
		ProcessingUnitExample p2 = new ProcessingUnitExample("p2", q2, q3);
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(p0);
		exec.execute(p1);
		exec.execute(p2);
			
		for(Integer i = 0; i < 100000000; i++)
		{
			Date date = new Date();
			VtcTimeTaggedEvent event = new VtcTimeTaggedEvent(i, date); 
			try	
			{	 
				q0.forcePut(event);
				TimeUnit.SECONDS.sleep(1000);
			}
			catch(InterruptedException e)
			{
				System.out.print("Interrupted during sleep in test()");
			}
		}
		exec.shutdownNow();
	}
	
	static void test2() 
	{
		// Unlimited size blocking queues
		VtcArrayBlockingQueue q1 = new VtcArrayBlockingQueue(5);
		XlsGoogleDateOpenPU p0 = new XlsGoogleDateOpenPU("p0", "C:\\Users\\ildefons\\vtc\\vtctest\\test\\googleimp.xls", q1);
		VtcArrayBlockingQueue q2 = new VtcArrayBlockingQueue(100);
		ProcessingUnitExample p1 = new ProcessingUnitExample("p1", q1, q2);
		VtcArrayBlockingQueue q3 = new VtcArrayBlockingQueue(100);
		ProcessingUnitExample p2 = new ProcessingUnitExample("p2", q2, q3);
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		exec.execute(p0);
		exec.execute(p1);
		exec.execute(p2);
			
		try
		{
			TimeUnit.SECONDS.sleep(2);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in test2()");
		}
		exec.shutdownNow();
		Date[] dateVector = q3.getLastTimeTags(150);
		for(Integer i = 0; i < dateVector.length; i++)
		{
			System.out.print("position " + i + " " + dateVector[i].toString() + "\n");
		}
	}
	
	static void test3()
	{
		VtcArrayBlockingQueue q = new VtcArrayBlockingQueue(200);
		for(Integer i = 0; i < 100; i++)
		{
			Date date = new Date();
			VtcTimeTaggedEvent event = new VtcTimeTaggedEvent(i, date); 
			try
			{
				q.forcePut(event);
			}
			catch(InterruptedException e)
			{
				System.out.print("Interrupted during forcePut(event) in test3()");
			}
		}
		
		Iterator<VtcTimeTaggedEvent> iter = q.getQueue().iterator();
		while(iter.hasNext())
		{
			Object o = iter.next().getEvent();
			Integer i = 0;
			Double d = 0.0;
			try
			{
				i = (Integer)o;
				d = (Double)o;
			}
			catch(ClassCastException e)
			{
				System.out.print("Class cast exception: " + e.getMessage());
			}
			System.out.print("Value: " + i + "\n");
		}
		
	}
	
	public static void main(String[] args) 
	{
		//backtest2();
		test16();
	}
} ///:-